﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CUiFLEXAttachmentEditorWindow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.Flexibles;
using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;
using UiInternalControls.WPF;


namespace DnProcessSimulateCommands;

public class CUiFLEXAttachmentEditorWindow : TxWindow, ICApFLEXFrameSelection, IComponentConnector
{
  internal CUiFLEXAttachmentEditorWindow dAttachmentDialog_;
  internal TextBlock AttachmentTypeLabel;
  internal System.Windows.Controls.ComboBox AttachmentTypeCombo;
  internal StackPanel FixedAttachmentStackPanel;
  internal CApCableFixedAttachmentControl FixedAttachmentControl;
  internal StackPanel RotatingAttachmentStackPanel;
  internal CApCableRotatingAttachmentControl RotatingAttachmentControl;
  internal StackPanel RotatingSlidingAttachmentStackPanel;
  internal CApCableRotatingSlidingAttachmentControl RotatingSlidingAttachmentControl;
  internal StackPanel RetractSystemAttachmentType1StackPanel;
  internal CApCableRetractSystemAttachmentType1Control RetractSystemAttachmentType1Control;
  internal StackPanel RetractSystemAttachmentType2StackPanel;
  internal CApCableRetractSystemAttachmentType2Control RetractSystemAttachmentType2Control;
  internal StackPanel RetractSystemAttachmentType3StackPanel;
  internal CApCableRetractSystemAttachmentType3Control RetractSystemAttachmentType3Control;
  internal Canvas PreviewButtonCanvas;
  internal TextBlock previewAttachmentAnimation;
  internal StackPanel ButtonsStackPanel;
  internal System.Windows.Controls.Button btnOk_;
  internal System.Windows.Controls.Button btnClose_;
  internal Image iMessageType_;
  internal TextBlock tbMessage_;
  private bool _contentLoaded;

  internal CUiFLEXAttachmentEditorWindow(
    CApCableFrameAttachmentRetriever attachmentRetriever,
    ICApFLEXCableAttributeViewModel attributeViewModel,
    CApFLEXCableAttachmentViewModel attachmentViewModel,
    CApFlexParameterController parameterController)
  {
    this.CanUpdateErrorMessage = false;
    this.IsCreated = false;
    this.IsCreationMode = true;
    this.ParameterController = parameterController;
    this.AttributeViewModel = attributeViewModel;
    this.AttachmentViewModel = attachmentViewModel;
    this.AttachmentCurrentlyEdited = (CApFLEXAttachment) null;
    this.CableAttachmentStatusAnalysis = new CApCableAttachmentStatusAnalysis(this.AttributeViewModel, this.AttachmentViewModel, attachmentRetriever);
    this.CableAttachmentStatusMessageFactory = new CApCableAttachmentStatusMessageFactory(this.CableAttachmentStatusAnalysis);
    this.CreateDialog();
  }

  internal CUiFLEXAttachmentEditorWindow(
    CApCableFrameAttachmentRetriever attachmentRetriever,
    ICApFLEXCableAttributeViewModel attributeViewModel,
    CApFLEXCableAttachmentViewModel attachmentViewModel,
    CApFlexParameterController parameterController,
    CApFLEXAttachment attachment)
  {
    this.CanUpdateErrorMessage = false;
    this.IsCreated = false;
    this.IsCreationMode = false;
    this.UpdateMinMaxWindowHeight = false;
    this.ParameterController = parameterController;
    this.AttributeViewModel = attributeViewModel;
    this.AttachmentViewModel = attachmentViewModel;
    this.AttachmentCurrentlyEdited = attachment;
    this.AttributeViewModel.RemoveAttachment(attachment);
    this.Attachment = attachment;
    this.AttachmentViewModel.AttachmentType = attachment.AttachmentType;
    this.CableAttachmentStatusAnalysis = new CApCableAttachmentStatusAnalysis(this.AttributeViewModel, this.AttachmentViewModel, attachmentRetriever);
    this.CableAttachmentStatusMessageFactory = new CApCableAttachmentStatusMessageFactory(this.CableAttachmentStatusAnalysis);
    this.CreateDialog();
  }

  public bool IsCreationMode { get; set; }

  public bool IsCreated { get; set; }

  public bool CanUpdateErrorMessage { get; set; }

  private ICUiFLEXAttachmentEditControl CurrentActiveControl { get; set; }

  public bool UpdateMinMaxWindowHeight { get; set; }

  public ICApFLEXCableAttributeViewModel AttributeViewModel { get; set; }

  public CApFLEXCableAttachmentViewModel AttachmentViewModel { get; set; }

  public CApFLEXAttachmentEditorViewModel EditorViewModel
  {
    get => this.CurrentActiveControl.EditorViewModel;
  }

  public TxObjEditBoxControl FrameEditBoxCtrlAttachment
  {
    get
    {
      return this.CurrentActiveControl != null ? this.CurrentActiveControl.FrameEditBoxCtrlAttachment : (TxObjEditBoxControl) null;
    }
  }

  private CApCableAttachmentStatusMessageFactory CableAttachmentStatusMessageFactory { get; set; }

  public CApCableAttachmentStatusAnalysis CableAttachmentStatusAnalysis { get; set; }

  public CApFLEXAttachment Attachment { get; set; }

  public CApFLEXAttachment AttachmentCurrentlyEdited { get; set; }

  private CApFLEXFrameValidator FrameValidator { get; set; }

  private bool IsWindowLoading { get; set; }

  private ITxLocatableObject AttachmentFrame { get; set; }

  private ITxLocatableObject SecondaryAttachmentFrame { get; set; }

  private CApFlexParameterController ParameterController { get; set; }

  private List<Storyboard> AttachmentStoryboards => this.CurrentActiveControl.AttachmentStoryboards;

  private Storyboard CurrentAttachmentStoryboard
  {
    get => this.CurrentActiveControl.ActiveAttachmentStoryboard;
  }

  private Shape CurrentSimulatedShape => this.CurrentActiveControl.SimulatedShape;

  private CApCableAttachmentFrameController AttachmentFrameController { get; set; }

  private List<ICUiFLEXAttachmentEditControl> AttachmentUserControls
  {
    get
    {
      return new List<ICUiFLEXAttachmentEditControl>(5)
      {
        (ICUiFLEXAttachmentEditControl) this.FixedAttachmentControl,
        (ICUiFLEXAttachmentEditControl) this.RotatingAttachmentControl,
        (ICUiFLEXAttachmentEditControl) this.RotatingSlidingAttachmentControl,
        (ICUiFLEXAttachmentEditControl) this.RetractSystemAttachmentType1Control,
        (ICUiFLEXAttachmentEditControl) this.RetractSystemAttachmentType2Control,
        (ICUiFLEXAttachmentEditControl) this.RetractSystemAttachmentType3Control
      };
    }
  }

  private void CreateDialog()
  {
    this.IsWindowLoading = true;
    this.InitializeComponent();
    this.InitializeControls();
    if (this.IsCreationMode)
      this.SetCurrentControl(CApFLEXAttachmentType.Fixed);
    else
      this.SetCurrentControl(this.Attachment.AttachmentType);
    this.InitTitleDialog();
    this.InitAttachmentTypeCombo();
  }

  private void TxWindow_Loaded(object sender, RoutedEventArgs e)
  {
    // ISSUE: explicit non-virtual call
    double height = __nonvirtual (((FrameworkElement) this).Height);
    ((FrameworkElement) this).MinHeight = height;
    ((FrameworkElement) this).MaxHeight = height;
    if (!this.IsCreationMode && this.Attachment != null)
      this.UpdateAttachmentTypeCombo(this.Attachment.AttachmentType);
    else
      this.UpdateAttachmentTypeCombo(CApFLEXAttachmentType.Fixed);
    this.UpdateCurrentControl();
    this.RegisterUserControlEvent();
    this.AttachmentViewModel.PropertyChanged += new PropertyChangedEventHandler(this.AttachmentViewModel_PropertyChanged);
    TxApplication.Options.Units.LinearUnitsChanged += new TxUnitsOptions_LinearUnitsChangedEventHandler(this.Units_LinearUnitsChanged);
    this.IsWindowLoading = false;
  }

  private void TxWindow_Closed(object sender, CancelEventArgs e)
  {
    this.UnregisterUserControlEvent();
    this.UnregisterAttachmentFrameNotifications();
    this.CloseUserControls();
    this.AttachmentViewModel.PropertyChanged -= new PropertyChangedEventHandler(this.AttachmentViewModel_PropertyChanged);
    TxApplication.Options.Units.LinearUnitsChanged -= new TxUnitsOptions_LinearUnitsChangedEventHandler(this.Units_LinearUnitsChanged);
    this.MakeChangesBeforeClosing();
  }

  private void RegisterUserControlEvent()
  {
    foreach (ICUiFLEXAttachmentEditControl attachmentUserControl in this.AttachmentUserControls)
      attachmentUserControl.ParameterChanged += new ParameterChangeEventHandler(this.UserControlParameterChanged);
  }

  private void UnregisterUserControlEvent()
  {
    foreach (ICUiFLEXAttachmentEditControl attachmentUserControl in this.AttachmentUserControls)
      attachmentUserControl.ParameterChanged -= new ParameterChangeEventHandler(this.UserControlParameterChanged);
  }

  private void UserControlParameterChanged() => this.ParameterChanged();

  private void SetEnabledDialogControls(bool status)
  {
    this.AttachmentTypeCombo.IsEnabled = status;
    this.ButtonsStackPanel.IsEnabled = status;
    this.CurrentActiveControl.SetEnabledAllParametersControls(status);
  }

  private void InitializeControls()
  {
    this.AttachmentFrameController = new CApCableAttachmentFrameController(this.CableAttachmentStatusAnalysis);
    this.SetIsControlLoading(true);
    this.FrameValidator = new CApFLEXFrameValidator((ICApFLEXFrameSelection) this, this.AttributeViewModel.CableScopeStatus);
    foreach (ICUiFLEXAttachmentEditControl attachmentUserControl in this.AttachmentUserControls)
      attachmentUserControl.InitControl(this.AttributeViewModel, this.AttachmentViewModel, this.FrameValidator, this.IsCreationMode, this.ParameterController, this.CableAttachmentStatusAnalysis, this.AttachmentFrameController);
  }

  private void InitAttachmentTypeCombo()
  {
    ObservableCollection<CApCableAttachmentTypeComboBoxItem> observableCollection = new ObservableCollection<CApCableAttachmentTypeComboBoxItem>();
    observableCollection.Add(new CApCableAttachmentTypeComboBoxItem(CApFLEXStringTable.AttachmentTypeFixed, CApFLEXAttachmentType.Fixed));
    observableCollection.Add(new CApCableAttachmentTypeComboBoxItem(CApFLEXStringTable.AttachmentTypeRotating, CApFLEXAttachmentType.Rotating));
    observableCollection.Add(new CApCableAttachmentTypeComboBoxItem(CApFLEXStringTable.AttachmentTypeRotatingSliding, CApFLEXAttachmentType.RotatingSliding));
    observableCollection.Add(new CApCableAttachmentTypeComboBoxItem(CApFLEXStringTable.AttachmentTypeRetractSystemType1, CApFLEXAttachmentType.RetractSystemType1));
    observableCollection.Add(new CApCableAttachmentTypeComboBoxItem(CApFLEXStringTable.AttachmentTypeRetractSystemType2, CApFLEXAttachmentType.RetractSystemType2));
    observableCollection.Add(new CApCableAttachmentTypeComboBoxItem(CApFLEXStringTable.AttachmentTypeRetractSystemType3, CApFLEXAttachmentType.RetractSystemType3));
    this.AttachmentTypeCombo.ItemsSource = (IEnumerable) observableCollection;
  }

  private void InitTitleDialog()
  {
    if (this.IsCreationMode)
    {
      // ISSUE: explicit non-virtual call
      __nonvirtual (((Window) this).Title) = CApFLEXStringTable.CreateAttachmentWindowTitle;
    }
    else
    {
      // ISSUE: explicit non-virtual call
      __nonvirtual (((Window) this).Title) = CApFLEXStringTable.EditAttachmentWindowTitle;
    }
  }

  private void InitAttachmentParameters()
  {
    if (!this.IsCreationMode)
    {
      this.CurrentActiveControl.UpdateAttachmentParametersWithAttachment(this.AttachmentCurrentlyEdited);
    }
    else
    {
      this.AttachmentViewModel.Location = this.ParameterController.AttachmentLocation;
      this.AttachmentViewModel.LengthFromStart = this.AttributeViewModel.Length * 0.5;
      this.AttachmentViewModel.SecondaryLengthFromStart = this.AttributeViewModel.Length * 0.25;
      this.AttachmentViewModel.RotatingRotationAxis = this.ParameterController.RotatingRotationAxis;
      this.AttachmentViewModel.RotatingSlidingRotationAxis = this.ParameterController.RotatingSlidingRotationAxis;
      this.AttachmentViewModel.SpringConstant = this.ParameterController.RetractSystemSpringConstant;
      this.AttachmentViewModel.MinRetractedLength = this.ParameterController.RetractSystemMinRetractLength;
      this.AttachmentViewModel.MaxRetractedLength = this.ParameterController.RetractSystemMaxRetractLength;
      this.CurrentActiveControl.UpdateAttachmentParametersWithParameters();
    }
  }

  private void UpdateParameterValues()
  {
    this.ParameterController.AttachmentLocation = this.AttachmentViewModel.Location;
    this.ParameterController.RotatingRotationAxis = this.AttachmentViewModel.RotatingRotationAxis;
    this.ParameterController.RotatingSlidingRotationAxis = this.AttachmentViewModel.RotatingSlidingRotationAxis;
    this.ParameterController.RetractSystemSpringConstant = this.AttachmentViewModel.SpringConstant;
    this.ParameterController.RetractSystemMinRetractLength = this.AttachmentViewModel.MinRetractedLength;
    this.ParameterController.RetractSystemMaxRetractLength = this.AttachmentViewModel.MaxRetractedLength;
  }

  private void UpdateStatusBarMessage()
  {
    if (!this.CanUpdateErrorMessage)
      return;
    this.AttachmentViewModel.AttachmentMessage = "";
    this.AttachmentViewModel.AttachmentStatusMessageType = CApCableStatusMessageType.None;
    if (this.CableAttachmentStatusMessageFactory != null)
    {
      CApCableStatusMessage priorityErrorMessage = this.CableAttachmentStatusMessageFactory.ComputeAttachmentPriorityErrorMessage();
      this.AttachmentViewModel.AttachmentMessage = priorityErrorMessage.MessageText;
      this.AttachmentViewModel.AttachmentStatusMessageType = priorityErrorMessage.MessageType;
    }
    this.UpdateOkSensitivity(this.AttachmentViewModel.AttachmentStatusMessageType);
  }

  private void UpdateAttachmentTypeCombo(CApFLEXAttachmentType attachmentType)
  {
    foreach (object obj in (IEnumerable) this.AttachmentTypeCombo.Items)
    {
      if (obj is CApCableAttachmentTypeComboBoxItem typeComboBoxItem && typeComboBoxItem.AttachmentType == attachmentType)
      {
        this.AttachmentTypeCombo.SelectedItem = (object) typeComboBoxItem;
        break;
      }
    }
  }

  private void UpdateAttachmentParameters()
  {
    if (!this.IsCreationMode && this.AttachmentViewModel.AttachmentType == this.AttachmentCurrentlyEdited.AttachmentType)
      this.CurrentActiveControl.UpdateAttachmentParametersWithAttachment(this.AttachmentCurrentlyEdited);
    else
      this.CurrentActiveControl.UpdateAttachmentParametersWithParameters();
  }

  private void StartUndoTransaction() => TxApplication.ActiveUndoManager.StartTransaction();

  private void EndUndoTransaction() => TxApplication.ActiveUndoManager.EndTransaction();

  public virtual void Undo()
  {
  }

  private void ParameterChanged()
  {
    if (this.IsWindowLoading)
      return;
    this.UpdateStatusBarMessage();
  }

  private void EnableOkButton()
  {
    if (this.AttachmentViewModel.IsOkEnabled)
      return;
    this.AttachmentViewModel.IsOkEnabled = this.AttributeViewModel.AreEquipmentParametersEnabled;
  }

  private void DisableOkButton()
  {
    if (!this.AttachmentViewModel.IsOkEnabled)
      return;
    this.AttachmentViewModel.IsOkEnabled = false;
  }

  private void UpdateOkSensitivity(CApCableStatusMessageType messageType)
  {
    switch (messageType)
    {
      case CApCableStatusMessageType.Error:
      case CApCableStatusMessageType.Incomplet:
        this.DisableOkButton();
        break;
      case CApCableStatusMessageType.Warning:
      case CApCableStatusMessageType.None:
        if (!this.IsCreationMode && !this.HasAttachmentDialogParametersChanged())
        {
          this.DisableOkButton();
          break;
        }
        this.EnableOkButton();
        break;
    }
  }

  private bool HasAttachmentDialogParametersChanged()
  {
    bool flag1 = false;
    bool flag2;
    if (this.AttachmentCurrentlyEdited.AttachmentType != this.AttachmentViewModel.AttachmentType)
    {
      flag2 = true;
    }
    else
    {
      flag2 = ((flag1 ? 1 : 0) | (this.AttachmentCurrentlyEdited.FixedFrame != this.AttachmentViewModel.FixedFrame ? 1 : (this.AttachmentCurrentlyEdited.LengthFromStart != this.AttachmentViewModel.LengthFromStart ? 1 : 0))) != 0;
      if (!flag2)
      {
        if (this.AttachmentCurrentlyEdited.AttachmentType == CApFLEXAttachmentType.Rotating)
        {
          CApFLEXRotatingAttachment attachmentCurrentlyEdited = this.AttachmentCurrentlyEdited as CApFLEXRotatingAttachment;
          flag2 = ((flag2 ? 1 : 0) | (attachmentCurrentlyEdited.Location != this.AttachmentViewModel.Location ? 1 : (attachmentCurrentlyEdited.RotationAxis != this.AttachmentViewModel.RotatingRotationAxis ? 1 : 0))) != 0;
        }
        else if (this.AttachmentCurrentlyEdited.AttachmentType == CApFLEXAttachmentType.RotatingSliding)
        {
          CApFLEXRotatingSlidingAttachment attachmentCurrentlyEdited = this.AttachmentCurrentlyEdited as CApFLEXRotatingSlidingAttachment;
          flag2 |= attachmentCurrentlyEdited.RotationAxis != this.AttachmentViewModel.RotatingSlidingRotationAxis;
        }
        else if (this.AttachmentCurrentlyEdited.AttachmentType == CApFLEXAttachmentType.RetractSystemType1)
        {
          CApFLEXRetractSystemAttachmentType1 attachmentCurrentlyEdited = this.AttachmentCurrentlyEdited as CApFLEXRetractSystemAttachmentType1;
          flag2 = ((flag2 ? 1 : 0) | (attachmentCurrentlyEdited.SpringConstant != this.AttachmentViewModel.SpringConstant ? 1 : (attachmentCurrentlyEdited.MaxRetractedLength != this.AttachmentViewModel.MaxRetractedLength ? 1 : 0))) != 0;
        }
        else if (this.AttachmentCurrentlyEdited.AttachmentType == CApFLEXAttachmentType.RetractSystemType2)
        {
          CApFLEXRetractSystemAttachmentType2 attachmentCurrentlyEdited = this.AttachmentCurrentlyEdited as CApFLEXRetractSystemAttachmentType2;
          flag2 = ((flag2 ? 1 : 0) | (attachmentCurrentlyEdited.SpringConstant != this.AttachmentViewModel.SpringConstant || attachmentCurrentlyEdited.MinRetractedLength != this.AttachmentViewModel.MinRetractedLength ? 1 : (attachmentCurrentlyEdited.MaxRetractedLength != this.AttachmentViewModel.MaxRetractedLength ? 1 : 0))) != 0;
        }
        else if (this.AttachmentCurrentlyEdited.AttachmentType == CApFLEXAttachmentType.RetractSystemType3)
        {
          CApFLEXRetractSystemAttachmentType3 attachmentCurrentlyEdited = this.AttachmentCurrentlyEdited as CApFLEXRetractSystemAttachmentType3;
          flag2 = ((flag2 ? 1 : 0) | (attachmentCurrentlyEdited.SecondaryFixedFrame != this.AttachmentViewModel.SecondaryFixedFrame || attachmentCurrentlyEdited.SecondaryLengthFromStart != this.AttachmentViewModel.SecondaryLengthFromStart || attachmentCurrentlyEdited.SpringConstant != this.AttachmentViewModel.SpringConstant || attachmentCurrentlyEdited.MinRetractedLength != this.AttachmentViewModel.MinRetractedLength ? 1 : (attachmentCurrentlyEdited.MaxRetractedLength != this.AttachmentViewModel.MaxRetractedLength ? 1 : 0))) != 0;
        }
      }
    }
    return flag2;
  }

  public bool IsFrameScopeValid(ITxLocatableObject locatableObject)
  {
    bool flag = true;
    if (this.AttachmentViewModel.Location == CApFLEXAttachmentLocation.AlongCable && this.AttributeViewModel.CableScopeStatus.IsCableInEquipment && !CApCableUtility.IsObjectInsideEquipment((ITxObject) locatableObject, this.AttributeViewModel.CableScopeStatus.ParentEquipment))
      flag = false;
    return flag;
  }

  public bool IsFrameAlreadyUsed(
    ITxLocatableObject locatableObject,
    out FrameSelectionType frameSelectionType)
  {
    bool flag = false;
    frameSelectionType = FrameSelectionType.None;
    if (((FrameworkElement) this).IsLoaded && (this.IsAttachmentFrameFocused() && this.AttachmentViewModel.Location == CApFLEXAttachmentLocation.AlongCable || this.IsSecondaryAttachmentFrameFocused() && this.AttachmentViewModel.AttachmentType == CApFLEXAttachmentType.RetractSystemType3))
    {
      if (locatableObject == this.AttributeViewModel.StartFrame)
      {
        frameSelectionType = FrameSelectionType.StartFrame;
        flag = true;
      }
      else if (locatableObject == this.AttributeViewModel.EndFrame)
      {
        frameSelectionType = FrameSelectionType.EndFrame;
        flag = true;
      }
      else if (this.IsAttachmentFrameFocused() && locatableObject == this.AttachmentViewModel.SecondaryFixedFrame || this.IsSecondaryAttachmentFrameFocused() && locatableObject == this.AttachmentViewModel.FixedFrame)
      {
        frameSelectionType = FrameSelectionType.AttachmentFrame;
        flag = true;
      }
      else
      {
        List<CApFLEXAttachment>.Enumerator enumerator = this.AttributeViewModel.RawAttachmentList().GetEnumerator();
        while (!flag && enumerator.MoveNext())
          flag = this.IsFrameAlreadyUsedAsAttachmentFrame(locatableObject, enumerator.Current);
        if (flag)
          frameSelectionType = FrameSelectionType.AttachmentFrame;
      }
    }
    return flag;
  }

  private bool IsFrameAlreadyUsedAsAttachmentFrame(
    ITxLocatableObject frame,
    CApFLEXAttachment attachment)
  {
    bool flag = attachment.Location == CApFLEXAttachmentLocation.AlongCable;
    if (flag)
    {
      flag = attachment.FixedFrame == frame;
      if (!flag && attachment is CApFLEXRetractSystemAttachmentType3 systemAttachmentType3)
        flag = systemAttachmentType3.SecondaryFixedFrame == frame;
    }
    return flag;
  }

  public bool IsAttachmentFrameFocused() => this.AttachmentViewModel.AttachmentFrameSelected;

  private bool IsSecondaryAttachmentFrameFocused()
  {
    return this.AttachmentViewModel.SecondaryAttachmentFrameSelected;
  }

  private void RegisterAttachmentFrameNotifications(ITxLocatableObject frame)
  {
    if (frame == null)
      return;
    frame.AbsoluteLocationChanged += new TxLocatableObject_AbsoluteLocationChangedEventHandler(this.Frame_AbsoluteLocationChanged);
    ((ITxObject) frame).NameChanged += new TxObject_NameChangedEventHandler(this.Frame_NameChanged);
    ((ITxObject) frame).Deleted += new TxObject_DeletedEventHandler(this.Frame_Deleted);
    frame.AttachmentParentChanged += new TxLocatableObject_AttachmentParentChangedEventHandler(this.Frame_AttachmentParentChanged);
  }

  private void UnregisterAttachmentFrameNotifications()
  {
    this.UnregisterAttachmentFrameNotifications(this.AttachmentFrame);
    this.UnregisterAttachmentFrameNotifications(this.SecondaryAttachmentFrame);
  }

  private void UnregisterAttachmentFrameNotifications(ITxLocatableObject frame)
  {
    if (frame == null)
      return;
    frame.AbsoluteLocationChanged -= new TxLocatableObject_AbsoluteLocationChangedEventHandler(this.Frame_AbsoluteLocationChanged);
    ((ITxObject) frame).NameChanged -= new TxObject_NameChangedEventHandler(this.Frame_NameChanged);
    ((ITxObject) frame).Deleted -= new TxObject_DeletedEventHandler(this.Frame_Deleted);
    frame.AttachmentParentChanged -= new TxLocatableObject_AttachmentParentChangedEventHandler(this.Frame_AttachmentParentChanged);
  }

  private void UpdateRegisterFrame()
  {
    this.UnregisterAttachmentFrameNotifications(this.AttachmentFrame);
    this.AttachmentFrame = this.AttachmentViewModel.FixedFrame;
    this.RegisterAttachmentFrameNotifications(this.AttachmentFrame);
  }

  private void UpdateRegisterSecondaryFrame()
  {
    this.UnregisterAttachmentFrameNotifications(this.SecondaryAttachmentFrame);
    this.SecondaryAttachmentFrame = this.AttachmentViewModel.SecondaryFixedFrame;
    this.RegisterAttachmentFrameNotifications(this.SecondaryAttachmentFrame);
  }

  private void Frame_Deleted(object sender, TxObject_DeletedEventArgs args)
  {
    if (!(sender is ITxLocatableObject itxLocatableObject) || itxLocatableObject != this.AttachmentViewModel.FixedFrame)
      return;
    this.AttachmentViewModel.FixedFrame = (ITxLocatableObject) null;
    this.FrameEditBoxCtrlAttachment.Object = (ITxObject) null;
    this.FrameEditBoxCtrlAttachment.Text = "";
    this.ParameterChanged();
  }

  private void Frame_AttachmentParentChanged(
    object sender,
    TxLocatableObject_AttachmentParentChangedEventArgs args)
  {
    if (sender == null)
      return;
    this.AttachmentParentFrameDidChanged(sender as ITxLocatableObject);
    this.ParameterChanged();
  }

  private void Frame_NameChanged(object sender, TxObject_NameChangedEventArgs args)
  {
    if (sender is ITxLocatableObject itxLocatableObject && itxLocatableObject == this.Attachment.FixedFrame)
      this.FrameEditBoxCtrlAttachment.Text = ((ITxObject) itxLocatableObject).Name;
    this.ParameterChanged();
  }

  private void Frame_AbsoluteLocationChanged(
    object sender,
    TxLocatableObject_AbsoluteLocationChangedEventArgs args)
  {
    this.ParameterChanged();
  }

  private void AttachmentParentFrameDidChanged(ITxLocatableObject obj)
  {
    this.CableAttachmentStatusAnalysis.CableFrameAttachmentRetriever.UpdateFrameAttachmentRegistry(obj);
  }

  private void AttachmentEditorLayoutUpdated()
  {
    if (!this.UpdateMinMaxWindowHeight)
      return;
    TxWindowBehavior.SetIsVerticallyResizable((DependencyObject) this, false);
    this.UpdateMinMaxHeight();
    this.UpdateMinMaxWindowHeight = false;
  }

  private void UpdateMinMaxHeight()
  {
    ((FrameworkElement) this).MaxHeight = double.PositiveInfinity;
    ((FrameworkElement) this).MinHeight = 0.0;
    ((Window) this).SizeToContent = SizeToContent.Height;
    ((UIElement) this).UpdateLayout();
    ((FrameworkElement) this).MaxHeight = ((FrameworkElement) this).MinHeight = ((FrameworkElement) this).ActualHeight;
  }

  private void btnOk_Click(object sender, RoutedEventArgs e)
  {
    try
    {
      CApCableUtility.UnHighlightObject((ITxDisplayableObject) (this.AttachmentViewModel.FixedFrame as TxFrame));
      switch (this.AttachmentViewModel.AttachmentType)
      {
        case CApFLEXAttachmentType.Fixed:
          this.Attachment = (CApFLEXAttachment) new CApFLEXFixedAttachment((ICApCableExtentDescriptor) this.AttributeViewModel);
          break;
        case CApFLEXAttachmentType.Rotating:
          CApFLEXRotatingAttachment rotatingAttachment = new CApFLEXRotatingAttachment((ICApCableExtentDescriptor) this.AttributeViewModel)
          {
            RotationAxis = this.AttachmentViewModel.RotatingRotationAxis
          };
          this.SetAttachmentLocation(rotatingAttachment);
          this.Attachment = (CApFLEXAttachment) rotatingAttachment;
          break;
        case CApFLEXAttachmentType.RotatingSliding:
          CApFLEXRotatingSlidingAttachment rotatingSlidingAttachment = new CApFLEXRotatingSlidingAttachment((ICApCableExtentDescriptor) this.AttributeViewModel)
          {
            RotationAxis = this.AttachmentViewModel.RotatingSlidingRotationAxis
          };
          this.SetAttachmentLocation(rotatingSlidingAttachment);
          this.Attachment = (CApFLEXAttachment) rotatingSlidingAttachment;
          break;
        case CApFLEXAttachmentType.RetractSystemType1:
          CApFLEXRetractSystemAttachmentType1 systemAttachmentType1 = new CApFLEXRetractSystemAttachmentType1((ICApCableExtentDescriptor) this.AttributeViewModel);
          systemAttachmentType1.SpringConstant = this.AttachmentViewModel.SpringConstant;
          systemAttachmentType1.MaxRetractedLength = this.AttachmentViewModel.MaxRetractedLength;
          this.Attachment = (CApFLEXAttachment) systemAttachmentType1;
          this.AttributeViewModel.RetractSystemCurrentMaxRetractedLength = this.AttachmentViewModel.MaxRetractedLength;
          break;
        case CApFLEXAttachmentType.RetractSystemType2:
          CApFLEXRetractSystemAttachmentType2 systemAttachmentType2 = new CApFLEXRetractSystemAttachmentType2((ICApCableExtentDescriptor) this.AttributeViewModel);
          systemAttachmentType2.SpringConstant = this.AttachmentViewModel.SpringConstant;
          systemAttachmentType2.MinRetractedLength = this.AttachmentViewModel.MinRetractedLength;
          systemAttachmentType2.MaxRetractedLength = this.AttachmentViewModel.MaxRetractedLength;
          this.Attachment = (CApFLEXAttachment) systemAttachmentType2;
          this.AttributeViewModel.RetractSystemCurrentMaxRetractedLength = this.AttachmentViewModel.MaxRetractedLength;
          break;
        case CApFLEXAttachmentType.RetractSystemType3:
          CApFLEXRetractSystemAttachmentType3 systemAttachmentType3 = new CApFLEXRetractSystemAttachmentType3((ICApCableExtentDescriptor) this.AttributeViewModel);
          systemAttachmentType3.SecondaryFixedFrame = this.AttachmentViewModel.SecondaryFixedFrame;
          systemAttachmentType3.SecondaryLengthFromStart = this.AttachmentViewModel.SecondaryLengthFromStart;
          systemAttachmentType3.SpringConstant = this.AttachmentViewModel.SpringConstant;
          systemAttachmentType3.MinRetractedLength = this.AttachmentViewModel.MinRetractedLength;
          systemAttachmentType3.MaxRetractedLength = this.AttachmentViewModel.MaxRetractedLength;
          this.Attachment = (CApFLEXAttachment) systemAttachmentType3;
          this.AttributeViewModel.RetractSystemCurrentMaxRetractedLength = this.AttachmentViewModel.MaxRetractedLength;
          break;
        default:
          throw new NotImplementedException();
      }
      this.Attachment.FixedFrame = this.AttachmentViewModel.FixedFrame;
      this.Attachment.LengthFromStart = this.GetActualLengthFromStart();
      this.UpdateParameterValues();
      this.IsCreated = true;
      ((Window) this).Close();
    }
    catch (Exception ex)
    {
      int num = (int) TxMessageBox.Show(CApFLEXStringTable.ATTACHMENT_CREATION_FAIL, CApFLEXStringTable.ATTACHMENT_ERROR_DIALOG_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
  }

  private void SetAttachmentLocation(CApFLEXRotatingAttachment rotatingAttachment)
  {
    switch (this.AttachmentViewModel.Location)
    {
      case CApFLEXAttachmentLocation.AlongCable:
        rotatingAttachment.SetAlongCable(this.AttachmentViewModel.FixedFrame, this.AttachmentViewModel.LengthFromStart);
        break;
      case CApFLEXAttachmentLocation.Start:
        rotatingAttachment.SetToStart();
        break;
      case CApFLEXAttachmentLocation.End:
        rotatingAttachment.SetToEnd();
        break;
      default:
        throw new NotImplementedException();
    }
  }

  private void SetAttachmentLocation(
    CApFLEXRotatingSlidingAttachment rotatingSlidingAttachment)
  {
    rotatingSlidingAttachment.SetAlongCable(this.AttachmentViewModel.FixedFrame, this.AttachmentViewModel.LengthFromStart);
  }

  private void btnCancel_Click(object sender, RoutedEventArgs e)
  {
    this.MakeChangesBeforeClosing();
    ((Window) this).Close();
  }

  private void MakeChangesBeforeClosing()
  {
    CApCableUtility.UnHighlightObject((ITxDisplayableObject) (this.AttachmentViewModel.FixedFrame as TxFrame));
    if (this.IsCreated || this.IsCreationMode)
      return;
    this.Attachment = this.AttachmentCurrentlyEdited;
    this.IsCreated = true;
  }

  private void statusbar_SizeChanged(object sender, SizeChangedEventArgs e)
  {
    if (this.IsWindowLoading)
      return;
    // ISSUE: explicit non-virtual call
    double num = (double) ((int) __nonvirtual (((FrameworkElement) this).Height) + ((int) e.NewSize.Height - (int) e.PreviousSize.Height));
    ((FrameworkElement) this).MinHeight = num;
    ((FrameworkElement) this).MaxHeight = num;
  }

  private void AttachmentEditor_LayoutUpdated(object sender, EventArgs e)
  {
    if (!this.UpdateMinMaxWindowHeight)
      return;
    TxWindowBehavior.SetIsVerticallyResizable((DependencyObject) this, false);
    this.UpdateMinMaxHeight();
    this.UpdateMinMaxWindowHeight = false;
  }

  private void AttachmentTypeCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    if (this.IsWindowLoading)
      return;
    if (this.AttachmentTypeCombo.SelectedItem != null)
    {
      this.SetCurrentControl((this.AttachmentTypeCombo.SelectedItem as CApCableAttachmentTypeComboBoxItem).AttachmentType);
      this.UpdateCurrentControl();
    }
    else
      this.UpdateAttachmentTypeCombo(this.AttachmentViewModel.AttachmentType);
    this.UpdateMinMaxWindowHeight = true;
    this.AttachmentEditorLayoutUpdated();
    if (this.AttachmentViewModel.IsEditableAttachment)
      return;
    this.AttachmentViewModel.ClearSelectionStatus();
  }

  public void SetCurrentControl(CApFLEXAttachmentType attachmentType)
  {
    this.AttachmentViewModel.AttachmentType = attachmentType;
    switch (attachmentType)
    {
      case CApFLEXAttachmentType.Rotating:
        this.CurrentActiveControl = (ICUiFLEXAttachmentEditControl) this.RotatingAttachmentControl;
        using (List<ICUiFLEXAttachmentEditControl>.Enumerator enumerator = this.AttachmentUserControls.GetEnumerator())
        {
          while (enumerator.MoveNext())
          {
            ICUiFLEXAttachmentEditControl current = enumerator.Current;
            if (!(current is CApCableRotatingAttachmentControl))
              current.EditorViewModel.IsControlActivated = false;
          }
          break;
        }
      case CApFLEXAttachmentType.RotatingSliding:
        this.CurrentActiveControl = (ICUiFLEXAttachmentEditControl) this.RotatingSlidingAttachmentControl;
        using (List<ICUiFLEXAttachmentEditControl>.Enumerator enumerator = this.AttachmentUserControls.GetEnumerator())
        {
          while (enumerator.MoveNext())
          {
            ICUiFLEXAttachmentEditControl current = enumerator.Current;
            if (!(current is CApCableRotatingSlidingAttachmentControl))
              current.EditorViewModel.IsControlActivated = false;
          }
          break;
        }
      case CApFLEXAttachmentType.RetractSystemType1:
        this.CurrentActiveControl = (ICUiFLEXAttachmentEditControl) this.RetractSystemAttachmentType1Control;
        using (List<ICUiFLEXAttachmentEditControl>.Enumerator enumerator = this.AttachmentUserControls.GetEnumerator())
        {
          while (enumerator.MoveNext())
          {
            ICUiFLEXAttachmentEditControl current = enumerator.Current;
            if (!(current is CApCableRetractSystemAttachmentType1Control))
              current.EditorViewModel.IsControlActivated = false;
          }
          break;
        }
      case CApFLEXAttachmentType.RetractSystemType2:
        this.CurrentActiveControl = (ICUiFLEXAttachmentEditControl) this.RetractSystemAttachmentType2Control;
        using (List<ICUiFLEXAttachmentEditControl>.Enumerator enumerator = this.AttachmentUserControls.GetEnumerator())
        {
          while (enumerator.MoveNext())
          {
            ICUiFLEXAttachmentEditControl current = enumerator.Current;
            if (!(current is CApCableRetractSystemAttachmentType2Control))
              current.EditorViewModel.IsControlActivated = false;
          }
          break;
        }
      case CApFLEXAttachmentType.RetractSystemType3:
        this.CurrentActiveControl = (ICUiFLEXAttachmentEditControl) this.RetractSystemAttachmentType3Control;
        using (List<ICUiFLEXAttachmentEditControl>.Enumerator enumerator = this.AttachmentUserControls.GetEnumerator())
        {
          while (enumerator.MoveNext())
          {
            ICUiFLEXAttachmentEditControl current = enumerator.Current;
            if (!(current is CApCableRetractSystemAttachmentType3Control))
              current.EditorViewModel.IsControlActivated = false;
          }
          break;
        }
      default:
        this.CurrentActiveControl = (ICUiFLEXAttachmentEditControl) this.FixedAttachmentControl;
        using (List<ICUiFLEXAttachmentEditControl>.Enumerator enumerator = this.AttachmentUserControls.GetEnumerator())
        {
          while (enumerator.MoveNext())
          {
            ICUiFLEXAttachmentEditControl current = enumerator.Current;
            if (!(current is CApCableFixedAttachmentControl))
              current.EditorViewModel.IsControlActivated = false;
          }
          break;
        }
    }
    this.EditorViewModel.IsControlActivated = true;
  }

  public void SetIsControlLoading(bool isLoading)
  {
    foreach (ICUiFLEXAttachmentEditControl attachmentUserControl in this.AttachmentUserControls)
      attachmentUserControl.EditorViewModel.IsControlLoading = isLoading;
  }

  public void UpdateCurrentControl()
  {
    this.FrameValidator.ShowError = this.IsCreationMode;
    this.UpdateAttachmentParameters();
    this.CurrentActiveControl.UpdateAttachmentCtrlBox();
    this.FrameValidator.ShowError = true;
    if (!this.AttachmentViewModel.IsEditableAttachment)
    {
      this.AttachmentViewModel.ClearSelectionStatus();
    }
    else
    {
      this.AttachmentTypeCombo.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
      this.CurrentActiveControl.SetDefaultFocus();
    }
    this.InitAttachmentStoryboards();
  }

  private void CloseUserControls()
  {
    List<ICUiFLEXAttachmentEditControl> attachmentUserControls = this.AttachmentUserControls;
    for (int index = 0; index < attachmentUserControls.Count; ++index)
    {
      attachmentUserControls[index].DidDialogClose();
      attachmentUserControls[index].EditorViewModel.IsControlActivated = false;
      ((FrameworkElement) this).RemoveLogicalChild((object) attachmentUserControls[index]);
      attachmentUserControls[index] = (ICUiFLEXAttachmentEditControl) null;
    }
  }

  private void TxNumericEditBoxAttachmentLength_TentativeValueChanged(
    object sender,
    TxNumericEditBoxCtrl_TentativeValueChangedEventArgs args)
  {
    if (this.IsWindowLoading)
      return;
    this.AttachmentViewModel.SetLengthFromStartNoEventSent(args.TentativeValue * TxApplication.Options.Units.LinearMultiplier);
    this.ParameterChanged();
  }

  private void Units_LinearUnitsChanged(
    object sender,
    TxUnitsOptions_LinearUnitsChangedEventArgs args)
  {
    this.CurrentActiveControl.UpdateLinearUnitText();
  }

  private void AttachmentViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
    if (this.AttachmentViewModel.IsCreationParameter(e.PropertyName))
      this.ParameterChanged();
    if (e.PropertyName == "FixedFrame")
    {
      this.UpdateRegisterFrame();
    }
    else
    {
      if (!(e.PropertyName == "SecondaryFixedFrame"))
        return;
      this.UpdateRegisterSecondaryFrame();
    }
  }

  private void previewAttachmentAnimation_PreviewMouseLeftButtonDown(
    object sender,
    MouseButtonEventArgs e)
  {
    if (this.EditorViewModel.IsPreviewRunning)
      return;
    this.EditorViewModel.IsPreviewRunning = true;
    this.SetEnabledDialogControls(false);
    this.CurrentAttachmentStoryboard.Completed += new EventHandler(this.sb_Completed_Backward);
    this.CurrentAttachmentStoryboard.Begin((FrameworkElement) this.CurrentSimulatedShape, true);
  }

  private void sb_Completed_Backward(object sender, EventArgs e)
  {
    this.CurrentAttachmentStoryboard.Completed -= new EventHandler(this.sb_Completed_Backward);
    this.CurrentAttachmentStoryboard.Remove();
    this.CurrentAttachmentStoryboard.Begin((FrameworkElement) this.CurrentSimulatedShape, true);
    this.CurrentAttachmentStoryboard.Pause((FrameworkElement) this.CurrentSimulatedShape);
    this.SetEnabledDialogControls(true);
    this.EditorViewModel.IsPreviewRunning = false;
  }

  private void AttachmentEditor_ContentRendered(object sender, EventArgs e)
  {
    this.SetIsControlLoading(false);
    this.CanUpdateErrorMessage = true;
    this.UpdateStatusBarMessage();
  }

  private void InitAttachmentStoryboards()
  {
    this.CurrentActiveControl.SetUpStoryboardsParameters();
    foreach (Storyboard attachmentStoryboard in this.AttachmentStoryboards)
    {
      attachmentStoryboard.Completed += new EventHandler(this.sb_Completed_Backward);
      attachmentStoryboard.Begin((FrameworkElement) this.CurrentSimulatedShape, true);
      attachmentStoryboard.Pause((FrameworkElement) this.CurrentSimulatedShape);
    }
  }

  private double GetActualLengthFromStart()
  {
    double actualLengthFromStart = -1.0;
    switch (this.AttachmentViewModel.Location)
    {
      case CApFLEXAttachmentLocation.AlongCable:
        actualLengthFromStart = this.AttachmentViewModel.LengthFromStart;
        break;
      case CApFLEXAttachmentLocation.Start:
        actualLengthFromStart = 0.0;
        break;
      case CApFLEXAttachmentLocation.End:
        actualLengthFromStart = this.AttributeViewModel.Length;
        break;
    }
    return actualLengthFromStart;
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/flexibles/windows/cuiflexattachmenteditorwindow.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  internal Delegate _CreateDelegate(Type delegateType, string handler)
  {
    return Delegate.CreateDelegate(delegateType, (object) this, handler);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.dAttachmentDialog_ = (CUiFLEXAttachmentEditorWindow) target;
        ((UIElement) this.dAttachmentDialog_).LayoutUpdated += new EventHandler(this.AttachmentEditor_LayoutUpdated);
        ((Window) this.dAttachmentDialog_).ContentRendered += new EventHandler(this.AttachmentEditor_ContentRendered);
        ((FrameworkElement) this.dAttachmentDialog_).Loaded += new RoutedEventHandler(this.TxWindow_Loaded);
        ((Window) this.dAttachmentDialog_).Closing += new CancelEventHandler(this.TxWindow_Closed);
        break;
      case 2:
        this.AttachmentTypeLabel = (TextBlock) target;
        break;
      case 3:
        this.AttachmentTypeCombo = (System.Windows.Controls.ComboBox) target;
        this.AttachmentTypeCombo.SelectionChanged += new SelectionChangedEventHandler(this.AttachmentTypeCombo_SelectionChanged);
        break;
      case 4:
        this.FixedAttachmentStackPanel = (StackPanel) target;
        break;
      case 5:
        this.FixedAttachmentControl = (CApCableFixedAttachmentControl) target;
        break;
      case 6:
        this.RotatingAttachmentStackPanel = (StackPanel) target;
        break;
      case 7:
        this.RotatingAttachmentControl = (CApCableRotatingAttachmentControl) target;
        break;
      case 8:
        this.RotatingSlidingAttachmentStackPanel = (StackPanel) target;
        break;
      case 9:
        this.RotatingSlidingAttachmentControl = (CApCableRotatingSlidingAttachmentControl) target;
        break;
      case 10:
        this.RetractSystemAttachmentType1StackPanel = (StackPanel) target;
        break;
      case 11:
        this.RetractSystemAttachmentType1Control = (CApCableRetractSystemAttachmentType1Control) target;
        break;
      case 12:
        this.RetractSystemAttachmentType2StackPanel = (StackPanel) target;
        break;
      case 13:
        this.RetractSystemAttachmentType2Control = (CApCableRetractSystemAttachmentType2Control) target;
        break;
      case 14:
        this.RetractSystemAttachmentType3StackPanel = (StackPanel) target;
        break;
      case 15:
        this.RetractSystemAttachmentType3Control = (CApCableRetractSystemAttachmentType3Control) target;
        break;
      case 16 /*0x10*/:
        this.PreviewButtonCanvas = (Canvas) target;
        break;
      case 17:
        ((UIElement) target).PreviewMouseLeftButtonDown += new MouseButtonEventHandler(this.previewAttachmentAnimation_PreviewMouseLeftButtonDown);
        break;
      case 18:
        this.previewAttachmentAnimation = (TextBlock) target;
        break;
      case 19:
        this.ButtonsStackPanel = (StackPanel) target;
        break;
      case 20:
        this.btnOk_ = (System.Windows.Controls.Button) target;
        this.btnOk_.Click += new RoutedEventHandler(this.btnOk_Click);
        break;
      case 21:
        this.btnClose_ = (System.Windows.Controls.Button) target;
        this.btnClose_.Click += new RoutedEventHandler(this.btnCancel_Click);
        break;
      case 22:
        ((FrameworkElement) target).SizeChanged += new SizeChangedEventHandler(this.statusbar_SizeChanged);
        break;
      case 23:
        this.iMessageType_ = (Image) target;
        break;
      case 24:
        this.tbMessage_ = (TextBlock) target;
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
